'use strict';

// const Controller = require('egg').Controller;

const Controller = require('./base');

class PrizeController extends Controller {

  // 界面列表
  async list() {
    const ctx = this.ctx;
    ctx.set('Content-Type', 'application/json');
    const list = await ctx.service.prize.list(ctx.query);
    this.success(list);
  }

  // 界面列表
  async prize() {
    const ctx = this.ctx;
    var params = ctx.params;
    ctx.set('Content-Type', 'application/json');
    if (!params.id) {
      this.ctx.throw(500, '参数错误');
    }
    const prize = await ctx.service.prize.find(params.id);
    this.success(prize);
  }

  // 添加奖品
  async create() {
    const ctx = this.ctx;
    var post = ctx.request.body;
    ctx.set('Content-Type', 'application/json');
    console.log(post);

    if (!post.name || post.types == null || post.num == "") {
      this.ctx.throw(500, '参数错误');
      this.ctx.body = {
        success: false,
        "msg": "参数错误"
      };
    } else {
      // 如果给了ID，就是更新，没有则添加
      if (!post.id) {
        const prize = await ctx.service.prize.create(post);
        this.success(prize);
      } else {
        const prize = await ctx.service.prize.update(post);
        this.success(prize);
      }
    }
  }

  // 删除界面
  async del() {
    const ctx = this.ctx;
    const id = ctx.request.body.id;
    ctx.set('Content-Type', 'application/json');
    const created = await ctx.service.prize.del(id);
    this.success({
      msg: "删除成功"
    })
  }

}

module.exports = PrizeController;
